package rule

import (
	
	"github.com/flipped-aurora/gin-vue-admin/server/global"
    "github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
    "github.com/flipped-aurora/gin-vue-admin/server/model/rule"
    ruleReq "github.com/flipped-aurora/gin-vue-admin/server/model/rule/request"
    "github.com/gin-gonic/gin"
    "go.uber.org/zap"
)

type RulesApi struct {}



// CreateRules 创建rules表
// @Tags Rules
// @Summary 创建rules表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body rule.Rules true "创建rules表"
// @Success 200 {object} response.Response{msg=string} "创建成功"
// @Router /rules/createRules [post]
func (rulesApi *RulesApi) CreateRules(c *gin.Context) {
	var rules rule.Rules
	err := c.ShouldBindJSON(&rules)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = rulesService.CreateRules(&rules)
	if err != nil {
        global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败:" + err.Error(), c)
		return
	}
    response.OkWithMessage("创建成功", c)
}

// DeleteRules 删除rules表
// @Tags Rules
// @Summary 删除rules表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body rule.Rules true "删除rules表"
// @Success 200 {object} response.Response{msg=string} "删除成功"
// @Router /rules/deleteRules [delete]
func (rulesApi *RulesApi) DeleteRules(c *gin.Context) {
	id := c.Query("id")
	err := rulesService.DeleteRules(id)
	if err != nil {
        global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败:" + err.Error(), c)
		return
	}
	response.OkWithMessage("删除成功", c)
}

// DeleteRulesByIds 批量删除rules表
// @Tags Rules
// @Summary 批量删除rules表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{msg=string} "批量删除成功"
// @Router /rules/deleteRulesByIds [delete]
func (rulesApi *RulesApi) DeleteRulesByIds(c *gin.Context) {
	ids := c.QueryArray("ids[]")
	err := rulesService.DeleteRulesByIds(ids)
	if err != nil {
        global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败:" + err.Error(), c)
		return
	}
	response.OkWithMessage("批量删除成功", c)
}

// UpdateRules 更新rules表
// @Tags Rules
// @Summary 更新rules表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body rule.Rules true "更新rules表"
// @Success 200 {object} response.Response{msg=string} "更新成功"
// @Router /rules/updateRules [put]
func (rulesApi *RulesApi) UpdateRules(c *gin.Context) {
	var rules rule.Rules
	err := c.ShouldBindJSON(&rules)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = rulesService.UpdateRules(rules)
	if err != nil {
        global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败:" + err.Error(), c)
		return
	}
	response.OkWithMessage("更新成功", c)
}

// FindRules 用id查询rules表
// @Tags Rules
// @Summary 用id查询rules表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query rule.Rules true "用id查询rules表"
// @Success 200 {object} response.Response{data=rule.Rules,msg=string} "查询成功"
// @Router /rules/findRules [get]
func (rulesApi *RulesApi) FindRules(c *gin.Context) {
	id := c.Query("id")
	rerules, err := rulesService.GetRules(id)
	if err != nil {
        global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败:" + err.Error(), c)
		return
	}
	response.OkWithData(rerules, c)
}
// GetRulesList 分页获取rules表列表
// @Tags Rules
// @Summary 分页获取rules表列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query ruleReq.RulesSearch true "分页获取rules表列表"
// @Success 200 {object} response.Response{data=response.PageResult,msg=string} "获取成功"
// @Router /rules/getRulesList [get]
func (rulesApi *RulesApi) GetRulesList(c *gin.Context) {
	var pageInfo ruleReq.RulesSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	list, total, err := rulesService.GetRulesInfoList(pageInfo)
	if err != nil {
	    global.GVA_LOG.Error("获取失败!", zap.Error(err))
        response.FailWithMessage("获取失败:" + err.Error(), c)
        return
    }
    response.OkWithDetailed(response.PageResult{
        List:     list,
        Total:    total,
        Page:     pageInfo.Page,
        PageSize: pageInfo.PageSize,
    }, "获取成功", c)
}

// GetRulesPublic 不需要鉴权的rules表接口
// @Tags Rules
// @Summary 不需要鉴权的rules表接口
// @accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{data=object,msg=string} "获取成功"
// @Router /rules/getRulesPublic [get]
func (rulesApi *RulesApi) GetRulesPublic(c *gin.Context) {
    // 此接口不需要鉴权
    // 示例为返回了一个固定的消息接口，一般本接口用于C端服务，需要自己实现业务逻辑
    rulesService.GetRulesPublic()
    response.OkWithDetailed(gin.H{
       "info": "不需要鉴权的rules表接口信息",
    }, "获取成功", c)
}
